<form id="formEdit" action="save" class="form-horizontal" role="form">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h4 class="modal-title" id="myModalLabel">操作</h4>
			</div>
			<div class="modal-body">

				<!-- 以下为要修改的的内容 -->

				<input type="hidden" name="id"
					data-th-value="${model==null?null:model.id}"> <input
					type="hidden" name="number"
					data-th-value="${model==null?null:model.number}">
				<div class="form-group">
					<label for="txt_year" class="col-sm-2 control-label">年级</label>
					<div class="col-sm-4">
						<select id="year" name="year" class="form-control"
							onchange="selectYear(this.value)">
							<option style="display: none"></option>
							<option data-th-each="year:${years}" data-th-value="${year}"
								data-th-text="${year}">1</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="txt_year" class="col-sm-2 control-label">学院</label>
					<div class="col-sm-4">
						<select class="form-control" id="college" name="college"
							onchange="selectCollege(this.value)">
							<option style="display: none"></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="txt_year" class="col-sm-2 control-label">专业</label>
					<div class="col-sm-4">
						<select class="form-control" id="profesion" name="profesion">
							<option style="display: none"></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="txt_name" class="col-sm-2 control-label">班级名字</label>
					<div class="col-sm-9">
						<input type="text" name="name"
							data-th-value=${model==null?null:model.name} class="form-control"
							id="name" placeholder="姓名">
					</div>
				</div>
				<!-- 以上为要修改的内容 -->

			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary">
					<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存
				</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">
					<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭
				</button>
			</div>
		</div>
	</div>
</form>
<script type="text/javascript" th:inline="javascript">
	var options = {
		message : '验证不通过',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {

			name : {
				validators : {
					notEmpty : {
						message : '班级名不能为空'
					}
				}
			}

		}
	};
	$('#formEdit').bootstrapValidator(options).on('success.form.bv',
			function(e) {
				e.preventDefault(); //很重要没有它则会提交默认表单，不会做下面的ajax表单提交
				var fd = new FormData($("#formEdit")[0]);
				$.ajax({
					url : "tbclass/save",
					type : "POST",
					data : fd,
					processData : false, // 不处理数据  
					contentType : false, // 不设置内容类型
					success : function(result) {
						if (result.success) {
							$('#myModal').modal("hide");
						} else {
							bootbox.alert(result.msg);
						}
					},
					error : function(result) {
						alert(result);
					}
				});
			});
</script>

<script type="text/javascript">
	$("#year").selectpicker({
		title : '选择年级'
	});
	$("#college").selectpicker({
		title : '选择学院'
	});
	$("#profesion").selectpicker({
		title : '选择专业'
	});
	
	function selectYear(year,collegeVal,profesionVal) {
		
		$.post('tbclass/getCollege', {
			year : year
		}, function(result) {
				$('#college,#profesion').html(
						'<option style="display: none"></option>');

				for (var i = 0; i < result.length; i++) {
					$('#college').append(
							"<option value='"+result[i]+"'>" + result[i]
									+ "</option>");
				}
				$('#college,#profesion').selectpicker('refresh');
				
				if(collegeVal!=null&&collegeVal!=undefined){
					$('#college').selectpicker('val',collegeVal);
					selectCollege(collegeVal,profesionVal);
					
				}
				
				//$(#profesion).selectpicker('val',$(this).find('option:first').val()); 
		})

	};
	
	
	
	function selectCollege(college,profesionVal) {
		var year = $('#year').val();
		$.post('tbclass/getProfesion', {
			college : college,
			year : year
		},
				function(result) {
						$('#profesion').html(
								'<option style="display: none"></option>');
						for (var i = 0; i < result.length; i++) {
							$('#profesion').append(
									"<option value='"+result[i]+"'>"
											+ result[i] + "</option>");
						}
						$('#profesion').selectpicker('refresh');
						if(profesionVal!=null&&profesionVal!=undefined){
							$('#profesion').selectpicker('val',profesionVal);
						}
				})
	};
	
	$(function(){
		if([[${model!=null}]]){
			var year = '[[${model==null?null:model.profession.year}]]';
			var college = '[[${model==null?null:model.profession.college}]]';
			var profesion = '[[${model==null?null:model.profession.profesion}]]';
			$('#year').selectpicker('val', year);
			selectYear(year,college,profesion);
		}else{
			var bjyear = $('#bjyear').val();
			var college = $('#search_college').val();
			var bjprofesion=$('#bjprofesion').val();
			$('#year').selectpicker('val', bjyear);
			console.log(bjyear+college+bjprofesion);
			selectYear(bjyear,college,bjprofesion);
		}
	})
	
</script>
